package com.jnzc.ocr.rest.app.shop.controller.app;

import com.google.common.collect.Lists;
import com.jnzc.ocr.common.bean.Page;
import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.param.app.AppOrderSaleDetailSaveParam;
import com.jnzc.ocr.rest.app.shop.param.app.AppOrderSaleSaveParam;
import com.jnzc.ocr.rest.app.shop.param.app.AppOrderSearchParam;
import com.jnzc.ocr.rest.app.shop.result.app.AppOrderSaleResult;
import com.jnzc.ocr.serv.shop.api.ShopOrderApi;
import com.jnzc.ocr.serv.shop.api.ShopOrderSaleApi;
import com.jnzc.ocr.serv.shop.api.ShopOrderSaleDetailApi;
import com.jnzc.ocr.serv.shop.consts.ShopOrderConsts;
import com.jnzc.ocr.serv.shop.consts.ShopOrderSaleConsts;
import com.jnzc.ocr.serv.shop.consts.ShopRefundConsts;
import com.jnzc.ocr.serv.shop.entity.ShopOrder;
import com.jnzc.ocr.serv.shop.entity.ShopOrderSale;
import com.jnzc.ocr.serv.shop.entity.ShopOrderSaleDetail;
import com.jnzc.ocr.serv.shop.filter.ShopOrderFilter;
import com.jnzc.ocr.serv.shop.vo.ShopOrderSaleCreateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017-10-06
 */
@Api(tags = {"app - 店铺订单销售表接口"})
@RestController
@RequestMapping("/app/shopOrderSale")
public class AppOrderSaleController extends BaseController {

    @Autowired
    private ShopOrderApi shopOrderApi;

    @Autowired
    private ShopOrderSaleApi shopOrderSaleApi;

    @Autowired
    private ShopOrderSaleDetailApi shopOrderSaleDetailApi;

    @ApiOperation(value = "分页")
    @GetMapping("/search")
    public PageResult<ShopOrder> orderSearch(AppOrderSearchParam param, Page page){
        UserVo userVo = super.get();
        ShopOrderFilter filter = new ShopOrderFilter();
        filter.setShopId(userVo.getShopId());
        filter.setType(ShopOrderConsts.TYPE_SALE);

        filter.setMemberId(param.getMemberId());
        filter.setState(param.getState());
        filter.setIfRefund(param.getIfRefund());
        filter.setBeginFinishTime(param.getBeginFinishTime());
        filter.setEndFinishTime(DateUtils.addDay(param.getEndFinishTime(), 1));
        switch (param.getSortField()) {
            case 1: filter.setOrderBy("create_time"); break;
            case 2: filter.setOrderBy("finish_time"); break;
            case 3: filter.setOrderBy("refund_time"); break;
            default: filter.setOrderBy("create_time");
        }
        if(param.getIfDesc()){
            filter.setOrderBy(filter.getOrderBy() + " desc");
        }
        return shopOrderApi.search(filter, page.getPageNo(), page.getPageSize());
    }

    @ApiOperation(value = "生成")
    @PostMapping("")
    public ShopOrder create(@RequestBody AppOrderSaleSaveParam param){
        UserVo userVo = super.get();
        ShopOrderSaleCreateVo createVo = new ShopOrderSaleCreateVo();
        ShopOrder order = new ShopOrder();
        order.setShopId(userVo.getShopId());
        order.setMemberId(param.getMemberId());
        order.setUserId(userVo.getId());
        order.setUserName(userVo.getName());
        createVo.setOrder(order);
        ShopOrderSale sale = new ShopOrderSale();
        sale.setSaleWay(ShopOrderSaleConsts.SALE_WAY_OFFLINE);
        createVo.setSale(sale);
        List<ShopOrderSaleDetail> saleDetailList = Lists.newArrayList();
        for (AppOrderSaleDetailSaveParam detailParam : param.getDetailList()) {
            ShopOrderSaleDetail obj = new ShopOrderSaleDetail();
            obj.setProductId(detailParam.getProductId());
            obj.setSkuId(detailParam.getSkuId());
            obj.setNum(detailParam.getNum());
            saleDetailList.add(obj);
        }
        createVo.setSaleDetailList(saleDetailList);
        createVo.setMarketingList(param.getMarketingList());
        return shopOrderSaleApi.create(createVo);
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path")
    })
    @GetMapping("/{id}")
    public AppOrderSaleResult info(@PathVariable Long id){
        UserVo userVo = super.get();
        AppOrderSaleResult result = new AppOrderSaleResult();
        ShopOrder order = shopOrderApi.find(id, userVo.getShopId());
        BusinessUtils.isTrue(ShopOrderConsts.TYPE_SALE == order.getType(), "订单不是销售订单");
        result.setOrder(order);
        ShopOrderSale sale = shopOrderSaleApi.find(id);
        result.setSale(sale);
        List<ShopOrderSaleDetail> saleDetailList = shopOrderSaleDetailApi.list(id);
        result.setSaleDetailList(saleDetailList);
        return result;
    }
}
